export default {
  data() {
    return {
      resizeHandler: null
    }
  },
  methods: {
    updateTableHeight(refName, extraHeight = 0) {
      const tableElement = this.$refs[refName]
      // 根据窗口大小计算表格高度
      const containerHeight = window.innerHeight - extraHeight
      this.tableHeight = `${containerHeight}px`
      // 设置表格高度
      tableElement.style.height = this.tableHeight
    },
    setupResizeListener(refName, extraHeight = 0) {
      if (this.resizeHandler) {
        window.removeEventListener('resize', this.resizeHandler)
      }
      this.resizeHandler = () => this.updateTableHeight(refName, extraHeight)
      window.addEventListener('resize', this.resizeHandler)
    },
    cleanupResizeListener() {
      if (this.resizeHandler) {
        window.removeEventListener('resize', this.resizeHandler)
        this.resizeHandler = null
      }
    }
  },
  mounted() {
    this.setupResizeListener()
  },
  beforeDestroy() {
    this.cleanupResizeListener()
  }
}
